package cn.hpclub.server.controller.client;

import org.json.JSONObject;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;

import cn.hpclub.server.constant.ClientApiConstant;
import cn.hpclub.server.interceptor.ClientApiIntercepter;
import cn.hpclub.server.service.MemberOrgService;

/**
 * 后台类 - 管理中心基类 继承此类的子类具备基本的CRUD
 * 
 */
@Before(value = { POST.class, ClientApiIntercepter.class })
public class BaseClientController<M extends Model<M>> extends Controller{

    public JSONObject setResponseObj(int code, String msg, Object data){
        JSONObject rjson = new JSONObject();

        rjson.put(ClientApiConstant.Key.RESULT_CODE, code);
        rjson.put(ClientApiConstant.Key.MESSAGE, msg);

        // if(!"".equals(data)){
        rjson.put(ClientApiConstant.Key.DATA, data);
        // }

        return rjson;
    }

    /**
     * 验证用户合法性，避免只传userId形成安全漏洞
     * 
     * @Title: isUserValid
     * @Description:TODO
     * @param userId
     * @param token
     * @return boolean
     */
    public boolean isUserValid(String userId, String token){
        boolean isValid = false;
        if(StrKit.notBlank(token) && token.length() >= 11){
            String phone = token.substring(token.length() - 11);
            String userName = MemberOrgService.service.getUserNameByUserId(userId);
            if(phone.equals(userName)){
                isValid = true;
            }
        }

        return isValid;
    }

}
